/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.secure.web.group;

import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.fyx.core.PermissionDeniedException;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.secure.controll.SecureGroupFacadeLocal;
import org.fyx.secure.entity.SecureGroup;
import org.fyx.secure.ejb.SecureLocal;
import org.fyx.secure.ejb.SecureRoles;
import org.fyx.util.FyxUtility;
import org.fyx.web.context.WebContext;

/**
 *
 * @author xljan
 */
@ManagedBean
@SessionScoped
public class SecureGroupList {

    private SecureGroup group;
    @EJB
    private SecureGroupFacadeLocal secureGroupFacade;
    @EJB
    private SecureLocal secure;
    @EJB
    private MulaLocal mula;

    @PostConstruct
    public void postConstruct(){
        FyxUtility.checkRole(SecureRoles.GROUP_MANAGER);
    }

    public SecureGroup getGroup() {
        return group;
    }

    public void setGroup(SecureGroup group) {
        this.group = group;
    }

    public Collection<SecureGroup> getAllGroups() {
        return secureGroupFacade.findAll();
    }

    public void remove() {
        if (group.getSecureUserCollection().size() > 0) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN,
                    mula.get(mula.get("Is not possible delete the group.")),
                    mula.get("The group has members. First delete all members of this group."));
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return;
        }
        secureGroupFacade.remove(group);
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                mula.get(mula.get("Deleting sucessfull.")), null);
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}
